package view;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;

import com.toedter.calendar.JDateChooser;

import model.Consorcio;
import model.ItemPresupuesto;
import model.Proveedor;

import controller.Sistema;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JRegistrarPresupuesto extends javax.swing.JFrame {
	private JPanel jPanel1;
	private JButton btnCancelar;
	private JLabel jLabel5;
	private JComboBox jComboConsorcios;
	private JLabel jLabel6;
	private JButton btnAgregar;
	private JTextField jTextField5;
	private JLabel jLabel8;
	private JTextArea jTextArea1;
	private JLabel jLabel7;
	private JComboBox ComboEstados;
	private JButton btnBuscar;
	private JTextField jTextField3;
	private JLabel jLabel4;
	private JComboBox ComboProveedores;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JButton btnAceptar;
	private JDateChooser fecha;
	private JDateChooser fechaValidez;
	public static JRegistrarPresupuesto instancia;
	
	//items
	private Vector<ItemPresupuesto> items;
	
	//combos
	private Vector<Consorcio> consorcios;
	private Vector<Proveedor> proveedores;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JRegistrarPresupuesto inst = new JRegistrarPresupuesto();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public JRegistrarPresupuesto() {
		super();
		initGUI();
		items = new Vector<ItemPresupuesto>();
	}
	
	public static JRegistrarPresupuesto getInstancia(){
		if (instancia == null)
			instancia = new JRegistrarPresupuesto();
		return instancia;
	}
	
	private void limpiarPantalla(){
		jTextField3.setText("");
		jTextField5.setText("");
		jTextArea1.setText("");
	}
	
	public void setNumeroReclamo(int num){
		try{
			System.out.print(num);	
			this.jTextField3.setText(String.valueOf(num));
					
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void limpiarPanel(){
		jTextArea1.setText("");
		jTextField5.setText("");
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Registrar Presupuesto");
			getContentPane().setLayout(null);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				jPanel1.setBounds(12, 143, 497, 169);
				jPanel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				jPanel1.setLayout(null);
				{
					jLabel7 = new JLabel();
					jPanel1.add(jLabel7);
					jLabel7.setText("Detalle");
					jLabel7.setBounds(18, 14, 96, 16);
				}
				{
					jTextArea1 = new JTextArea();
					jPanel1.add(jTextArea1);
					jTextArea1.setBounds(18, 36, 351, 59);
					jTextArea1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				}
				{
					jLabel8 = new JLabel();
					jPanel1.add(jLabel8);
					jLabel8.setText("Monto");
					jLabel8.setBounds(384, 14, 99, 16);
				}
				{
					jTextField5 = new JTextField();
					jPanel1.add(jTextField5);
					jTextField5.setBounds(381, 36, 102, 23);
				}
				{
					btnAgregar = new JButton();
					jPanel1.add(btnAgregar);
					btnAgregar.setText("Agregar");
					btnAgregar.setBounds(381, 71, 102, 23);
					btnAgregar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {							
							String detalle = jTextArea1.getText();
							float monto = Float.parseFloat(jTextField5.getText());
							ItemPresupuesto nuevo = new ItemPresupuesto(detalle, monto);
							items.add(nuevo);
							limpiarPanel();
						}
					});
				}
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(164, 328, 91, 23);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						boolean estado = false;
																
						if (ComboEstados.getSelectedIndex()==1){
							estado = true;
						}
						
						Sistema.getInstancia().registrarPresupuesto(jComboConsorcios.getSelectedItem().toString(), Integer.parseInt(jTextField3.getText()),fecha.getCalendar(), ComboProveedores.getSelectedItem().toString(), fechaValidez.getCalendar(), items, estado);
												
						limpiarPantalla();
						items.removeAllElements();
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(266, 328, 93, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("fecha");
				jLabel1.setBounds(22, 47, 48, 16);
				jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				Calendar hoy = Calendar.getInstance();
				fecha = new JDateChooser();
				fecha.setCalendar(hoy);
				getContentPane().add(fecha);
				fecha.setBounds(75, 45, 120, 23);
			}
			{
				Calendar fecha = Calendar.getInstance();
				fechaValidez = new JDateChooser();
				fechaValidez.setCalendar(fecha);
				getContentPane().add(fechaValidez);
				fechaValidez.setBounds(387, 45, 120, 23);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Fecha Validez");
				jLabel2.setBounds(296, 47, 90, 16);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Proveedor");
				jLabel3.setBounds(1, 78, 69, 16);
				jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				proveedores = Sistema.getInstancia().getProveedores();
				
				//Armar texto
				Vector<String> prov = new Vector<String>();
				if(proveedores!=null){
				for (int i=0;i<proveedores.size();i++){
					prov.add(proveedores.elementAt(i).getRazonSocial());	
				}
				
				}								
				ComboBoxModel ComboProveedoresModel = new DefaultComboBoxModel(prov);
				ComboProveedores = new JComboBox();
				getContentPane().add(ComboProveedores);
				ComboProveedores.setModel(ComboProveedoresModel);
				ComboProveedores.setBounds(75, 75, 228, 23);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Reclamo");
				jLabel4.setBounds(12, 108, 57, 16);
				jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				jTextField3 = new JTextField();
				getContentPane().add(jTextField3);
				jTextField3.setBounds(76, 107, 96, 23);
			}
			{
				btnBuscar = new JButton();
				getContentPane().add(btnBuscar);
				btnBuscar.setText("Buscar");
				btnBuscar.setBounds(184, 107, 119, 23);
				btnBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						JBuscarReclamo.getInstancia().setVisible(true);
						JBuscarReclamo.getInstancia().setLocationRelativeTo(null);
					}
				});
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5);
				jLabel5.setText("Estado");
				jLabel5.setBounds(315, 79, 50, 16);
			}
			{
				ComboBoxModel ComboEstadosModel = 
					new DefaultComboBoxModel(
							new String[] { "Pendiente", "Aprobado" });
				ComboEstados = new JComboBox();
				getContentPane().add(ComboEstados);
				ComboEstados.setModel(ComboEstadosModel);
				ComboEstados.setBounds(359, 76, 149, 23);
			}
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Consorcio");
				jLabel6.setBounds(7, 16, 63, 16);
				jLabel6.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				consorcios = Sistema.getInstancia().getConsorcios();
			
				//Armar texto
				Vector<String> nombres = new Vector<String>();
				if(consorcios!=null){
				for (int i=0;i<consorcios.size();i++)
					nombres.add(consorcios.elementAt(i).getNombre());						
								
				}										
				//ComboBoxModel jComboConsorciosModel = new DefaultComboBoxModel(new String[] {"Sin Conexion"});
										
				ComboBoxModel jComboConsorciosModel = new DefaultComboBoxModel(nombres);	
				jComboConsorcios = new JComboBox();
				getContentPane().add(jComboConsorcios);
				jComboConsorcios.setModel(jComboConsorciosModel);
				jComboConsorcios.setBounds(74, 13, 265, 23);
			}
			pack();
			this.setSize(537, 397);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
